<?php
	class FacebookHelper extends AppHelper
	{
		var $helpers = array("Javascript",'Html');
		function beforeRender()
		{
			if(cr("Facebook.noSelfInit")!==true && (!isset($this->params['isAjax']) || $this->params['isAjax']!= true))
			{
				$this->initFb();	
			}
		}
		
		function initFb()
		{
			$appId = cr("FacebookConfig.appId");
			if($appId)
			{
				$appId = $this->Javascript->object($appId);
				$session = FacebookApi::getInstance()->getUser();
				if(!$session)
				{
					$session = null;
				}
				$sessionJson  = json_encode($session);
				$onInit = cr("Facebook.onInitFn");
				if(!$onInit)
				{
					$onInit = "null";
				}
				$loginUrl = "null";
				if(cr("Facebook.loginUrl"))
				{
					$loginUrl = cr("Facebook.loginUrl");
					$loginUrl = $this->Html->url($loginUrl);
					$loginUrl = '"$loginUrl"';
				}
				$jsFile = '<script type="text/javascript" src="'.$this->Html->url('/sociable/js/fb.js').'"></script>';
				$this->getView()->addScript("FB_FILE",$jsFile);
				$initScript =<<<END
				FbHelper.APP_ID = $appId;
				FbHelper.loginUrl = $loginUrl; 
				FbHelper.initFb($sessionJson,$onInit) 
END;
				$this->Javascript->startup($initScript);
			}
		}
	}
?>